<!DOCTYPE html>
<html>
<head>
    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?48711b05b1c1178a5b74ec9dcd75c600"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    <meta name="baidu-site-verification" content="d0GGaTdNRH" />
    
    
    
    
    <title>creat-react-app 配置 | jscheng</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <!--<meta name="keywords" content="react">-->
    <meta name="keywords" content="creat-react-app 配置 | jscheng">

    <meta name="description" content="web前端,jscheng,blog,jscheng的博客，JSCheng">
<meta property="og:type" content="blog">
<meta property="og:title" content="creat-react-app 配置">
<meta property="og:url" content="https://www.jscheng.top/2020/06/02/react-init/index.html">
<meta property="og:site_name" content="jscheng">
<meta property="og:description" content="web前端,jscheng,blog,jscheng的博客，JSCheng">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-06-02T02:45:45.000Z">
<meta property="article:modified_time" content="2021-07-09T05:22:26.096Z">
<meta property="article:author" content="jscheng">
<meta property="article:tag" content="react">
<meta name="twitter:card" content="summary">

    
    <link rel="shortcut icon" href="/blog/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
    <style>
        .container{
            /* width: 1200px; */
        }
        *{
            box-sizing: border-box;
        }
    </style>
    <img src="https://jscheng-img.oss-cn-shanghai.aliyuncs.com/logo.png" alt="logo" hidden>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/brand.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.png">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">jscheng</h5>
          <a href="mailto:wangcheng55555@163.com" title="wangcheng55555@163.com" class="mail">wangcheng55555@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                目录
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                类别
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/about"  >
                <i class="icon icon-lg icon-link"></i>
                about
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">creat-react-app 配置</div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">creat-react-app 配置</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-06-02T02:45:45.000Z" itemprop="datePublished" class="page-time">
  2020-06-02
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E6%95%99%E7%A8%8B/">教程</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#环境变量-env"><span class="post-toc-number">1.</span> <span class="post-toc-text">环境变量 env</span></a></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#webpack"><span class="post-toc-number">2.</span> <span class="post-toc-text">webpack</span></a></li></ol>
        </nav>
    </aside>


<article id="post-react-init"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">creat-react-app 配置</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-06-02 10:45:45" datetime="2020-06-02T02:45:45.000Z"  itemprop="datePublished">2020-06-02</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E6%95%99%E7%A8%8B/">教程</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="环境变量-env"><a href="#环境变量-env" class="headerlink" title="环境变量 env"></a>环境变量 env</h1><p> 根目录下新建 .env.development  &amp;&amp; .env.production<br> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 以REACT_APP_ 开头</span><br><span class="line">&#x2F;&#x2F; 开发环境 PORT&#x3D;&quot;9999&quot;</span><br><span class="line">REACT_APP_xxxx_ROOT&#x3D;&quot;https:&#x2F;&#x2F;test-xxx.xxx.com&quot;</span><br><span class="line">REACT_APP_xxx_API_ROOT&#x3D;&quot;https:&#x2F;&#x2F;test-xxxx.xxx.com.cn&#x2F;&quot;</span><br><span class="line">REACT_APP_xxx_API_ROOT&#x3D;&quot;https:&#x2F;&#x2F;test-xxxxx.xxxx.com.cn&#x2F;&quot;</span><br></pre></td></tr></table></figure></p>
<h1 id="webpack"><a href="#webpack" class="headerlink" title="webpack"></a>webpack</h1><ol>
<li>安装 react-app-rewired<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">create-react-app 2.x with Webpack 4</span><br><span class="line">npm install react-app-rewired --save-dev</span><br><span class="line">create-react-app 1.x or react-scripts-ts with Webpack 3</span><br><span class="line">npm install react-app-rewired@1.6.2 --save-dev</span><br></pre></td></tr></table></figure></li>
<li>根目录创建 config-overrides.js<br>/* config-overrides.js */<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">module.exports &#x3D; function override(config, env) &#123;</span><br><span class="line">  &#x2F;&#x2F;do stuff with the webpack config...</span><br><span class="line">  return config;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
当然我们也可以把config-overrides.js放到其他位置，比如我们要指向node_modules中某个第三方库提供的配置文件，就可以添加下面配置到package.json：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;config-overrides-path&quot;: &quot;node_modules&#x2F;other-rewire&quot;</span><br></pre></td></tr></table></figure></li>
<li>替换 react-scripts</li>
</ol>
<p>打开package.json:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;* package.json *&#x2F;</span><br><span class="line"></span><br><span class="line">  &quot;scripts&quot;: &#123;</span><br><span class="line">-   &quot;start&quot;: &quot;react-scripts start&quot;,</span><br><span class="line">+   &quot;start&quot;: &quot;react-app-rewired start&quot;,</span><br><span class="line">-   &quot;build&quot;: &quot;react-scripts build&quot;,</span><br><span class="line">+   &quot;build&quot;: &quot;react-app-rewired build&quot;,</span><br><span class="line">-   &quot;test&quot;: &quot;react-scripts test --env&#x3D;jsdom&quot;,</span><br><span class="line">+   &quot;test&quot;: &quot;react-app-rewired test --env&#x3D;jsdom&quot;,</span><br><span class="line">    &quot;eject&quot;: &quot;react-scripts eject&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>配置<br>定制 Webpack 配置<br>webpack字段可以用来添加你的额外配置，当然这里面不包含Webpack Dev Server。</li>
</ol>
<p>安装less <a href="mailto:less-loader@5.0.0">less-loader@5.0.0</a> postcss-px-to-viewport customize-cra babel-plugin-import  autoprefixer<br>less-loader版本问题 报错Invalid options object. Less Loader has been initialized using an options object that does not ma…</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cnpm i less less-loader@5.0.0 postcss-px-to-viewport customize-cra babel-plugin-import  autoprefixer -D</span><br></pre></td></tr></table></figure>
<p>配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line">const &#123; override, overrideDevServer, fixBabelImports, addLessLoader, addWebpackAlias, addWebpackModuleRule &#125; &#x3D; require(&#39;customize-cra&#39;);</span><br><span class="line">const path &#x3D; require(&#39;path&#39;);</span><br><span class="line">const pxtovw &#x3D; require(&#39;postcss-px-to-viewport&#39;);</span><br><span class="line">const autoprefixer &#x3D; require(&#39;autoprefixer&#39;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">const removeManifest &#x3D; () &#x3D;&gt; config &#x3D;&gt; &#123;</span><br><span class="line">    config.plugins &#x3D; config.plugins.filter(</span><br><span class="line">        p &#x3D;&gt; p.constructor.name !&#x3D;&#x3D; &quot;ManifestPlugin&quot;</span><br><span class="line">    );</span><br><span class="line">    return config;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    webpack: override(</span><br><span class="line">        removeManifest(), &#x2F;&#x2F;删除manifest</span><br><span class="line">        fixBabelImports(&#39;import&#39;, &#123;  &#x2F;&#x2F;antd 按需加载less</span><br><span class="line">            libraryName: &#39;antd-mobile&#39;,</span><br><span class="line">            style: true,</span><br><span class="line">        &#125;),</span><br><span class="line">        addLessLoader(&#123; &#x2F;&#x2F;less配置antd主题色</span><br><span class="line">                    javascriptEnabled: true,</span><br><span class="line">                    modifyVars: &#123;</span><br><span class="line">                        &#39;@color-text-base&#39;:&#39;#333&#39;,</span><br><span class="line">                        &quot;@brand-primary&quot;: &quot;#2dcbb5&quot;,</span><br><span class="line">                        &quot;@brand-primary-tap&quot;: &quot;#57d5c4&quot;,</span><br><span class="line">                        &quot;@brand-success&quot;: &quot;#6abf47&quot;,</span><br><span class="line">                        &quot;@brand-warning&quot;: &quot;#ffc600&quot;,</span><br><span class="line">                        &quot;@brand-error&quot;: &quot;#f4333c&quot;,</span><br><span class="line">                        &quot;@brand-important&quot;: &quot;#ff5b05&quot;,</span><br><span class="line">                        &quot;@brand-wait&quot;: &quot;#2dcbb5&quot;,</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;),</span><br><span class="line">        addPostcssPlugins([ &#x2F;&#x2F;vw配置以及autoprefixer</span><br><span class="line">                    new pxtovw(&#123;</span><br><span class="line">                        unitToConvert: &#39;px&#39;, &#x2F;&#x2F;需要转换的单位，默认为&quot;px&quot;；</span><br><span class="line">                        viewportWidth: 375, &#x2F;&#x2F;设计稿的视口宽度</span><br><span class="line">                        unitPrecision: 5, &#x2F;&#x2F;单位转换后保留的小数位数</span><br><span class="line">                        propList: [&#39;*&#39;], &#x2F;&#x2F;要进行转换的属性列表,*表示匹配所有,!表示不转换</span><br><span class="line">                        viewportUnit: &#39;vw&#39;, &#x2F;&#x2F;转换后的视口单位</span><br><span class="line">                        fontViewportUnit: &#39;vw&#39;, &#x2F;&#x2F;转换后字体使用的视口单位</span><br><span class="line">                        selectorBlackList: [], &#x2F;&#x2F;不进行转换的css选择器，继续使用原有单位</span><br><span class="line">                        minPixelValue: 1, &#x2F;&#x2F;设置最小的转换数值</span><br><span class="line">                        mediaQuery: false, &#x2F;&#x2F;设置媒体查询里的单位是否需要转换单位</span><br><span class="line">                        replace: true, &#x2F;&#x2F;是否直接更换属性值，而不添加备用属性</span><br><span class="line">                        &#x2F;&#x2F; exclude: [&#x2F;node_modules&#x2F;] &#x2F;&#x2F;忽略某些文件夹下的文件</span><br><span class="line">                    &#125;),</span><br><span class="line">                    new autoprefixer(&#123;</span><br><span class="line">                        overrideBrowserslist: [</span><br><span class="line">                            &quot;Android 4.1&quot;,</span><br><span class="line">                            &quot;iOS 7.1&quot;,</span><br><span class="line">                            &quot;Chrome &gt; 31&quot;,</span><br><span class="line">                            &quot;ff &gt; 31&quot;,</span><br><span class="line">                            &quot;ie &gt;&#x3D; 8&quot;</span><br><span class="line">                        ]</span><br><span class="line">                    &#125;)</span><br><span class="line">        ]),</span><br><span class="line">        addWebpackExternals(&#123; &#x2F;&#x2F;不做打包处理配置，如直接以cdn引入的</span><br><span class="line">            &#x2F;&#x2F; highcharts:&quot;window.highcharts&quot;</span><br><span class="line">        &#125;),</span><br><span class="line"></span><br><span class="line">        addWebpackAlias(&#123; &#x2F;&#x2F;添加别名</span><br><span class="line">            &#39;@&#39;: path.resolve(__dirname, &#39;src&#39;)</span><br><span class="line">        &#125;),</span><br><span class="line">    ),</span><br><span class="line">    devServer: overrideDevServer(</span><br><span class="line"></span><br><span class="line">    )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>以下看看就好 =================<br>定制 Jest 配置 - Testing<br>jest配置</p>
<p>定制 Webpack Dev Server<br>通过devServer我们可以做一些开发环境的配置，比如设置proxy代理，调整publicPath，通过disableHostCheck禁用转发域名检查等。</p>
<p>从CRA 2.0开始，推荐搭配customize-cra使用，里面提供了一些常用的配置，可以方便我们直接使用。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">const &#123; override, overrideDevServer, &#125; &#x3D; require(&#39;customize-cra&#39;);</span><br><span class="line"></span><br><span class="line">const addProxy &#x3D; () &#x3D;&gt; (configFunction) &#x3D;&gt; &#123;</span><br><span class="line">    configFunction.proxy &#x3D; &#123;</span><br><span class="line">        &#39;&#x2F;v2ex&#x2F;&#39;: &#123;</span><br><span class="line">            target: &#39;https:&#x2F;&#x2F;www.v2ex.com&#39;,</span><br><span class="line">            changeOrigin: true,</span><br><span class="line">            pathRewrite: &#123; &#39;^&#x2F;v2ex&#39;: &#39;&#x2F;&#39; &#125;,</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    return configFunction;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    webpack: override(</span><br><span class="line">        ...</span><br><span class="line">    ),</span><br><span class="line">    devServer: overrideDevServer(</span><br><span class="line">        addProxy()</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Paths - 路径变量<br>paths里面是create-react-app里面的一些路径变量，包含打包目录、dotenv配置地址、html模板地址等。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">  dotenv: resolveApp(&#39;.env&#39;),</span><br><span class="line">  appPath: resolveApp(&#39;.&#39;),</span><br><span class="line">  appBuild: resolveApp(&#39;build&#39;),</span><br><span class="line">  appPublic: resolveApp(&#39;public&#39;),</span><br><span class="line">  appHtml: resolveApp(&#39;public&#x2F;index.html&#39;),</span><br><span class="line">  appIndexJs: resolveModule(resolveApp, &#39;src&#x2F;index&#39;),</span><br><span class="line">  appPackageJson: resolveApp(&#39;package.json&#39;),</span><br><span class="line">  appSrc: resolveApp(&#39;src&#39;),</span><br><span class="line">  appTsConfig: resolveApp(&#39;tsconfig.json&#39;),</span><br><span class="line">  appJsConfig: resolveApp(&#39;jsconfig.json&#39;),</span><br><span class="line">  yarnLockFile: resolveApp(&#39;yarn.lock&#39;),</span><br><span class="line">  testsSetup: resolveModule(resolveApp, &#39;src&#x2F;setupTests&#39;),</span><br><span class="line">  proxySetup: resolveApp(&#39;src&#x2F;setupProxy.js&#39;),</span><br><span class="line">  appNodeModules: resolveApp(&#39;node_modules&#39;),</span><br><span class="line">  publicUrl: getPublicUrl(resolveApp(&#39;package.json&#39;)),</span><br><span class="line">  servedPath: getServedPath(resolveApp(&#39;package.json&#39;)),</span><br><span class="line">  &#x2F;&#x2F; These properties only exist before ejecting:</span><br><span class="line">  ownPath: resolveOwn(&#39;.&#39;),</span><br><span class="line">  ownNodeModules: resolveOwn(&#39;node_modules&#39;), &#x2F;&#x2F; This is empty on npm 3</span><br><span class="line">  appTypeDeclarations: resolveApp(&#39;src&#x2F;react-app-env.d.ts&#39;),</span><br><span class="line">  ownTypeDeclarations: resolveOwn(&#39;lib&#x2F;react-app.d.ts&#39;),</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>比如我们要修改appHtml即html模板的默认位置，可以这样做：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">const path &#x3D; require(&#39;path&#39;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    paths: function (paths, env) &#123;</span><br><span class="line"></span><br><span class="line">        &#x2F;&#x2F; 指向根目录的test.html</span><br><span class="line">        paths.appHtml &#x3D; path.resolve(__dirname, &quot;test.html&quot;);</span><br><span class="line"></span><br><span class="line">        return paths;</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="5">
<li>常用示例<br>添加多页面入口<br>首先安装react-app-rewire-multiple-entry。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install react-app-rewire-multiple-entry --save-dev</span><br></pre></td></tr></table></figure>
然后在config-overrides.js配置：<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">const &#123; override, overrideDevServer &#125; &#x3D; require(&#39;customize-cra&#39;);</span><br><span class="line"></span><br><span class="line">const multipleEntry &#x3D; require(&#39;react-app-rewire-multiple-entry&#39;)([&#123;</span><br><span class="line">    entry: &#39;src&#x2F;pages&#x2F;options.tsx&#39;,</span><br><span class="line">    template: &#39;public&#x2F;options.html&#39;,</span><br><span class="line">    outPath: &#39;&#x2F;options.html&#39;,</span><br><span class="line">&#125;]);</span><br><span class="line"></span><br><span class="line">const addEntry &#x3D; () &#x3D;&gt; config &#x3D;&gt; &#123;</span><br><span class="line"></span><br><span class="line">    multipleEntry.addMultiEntry(config);</span><br><span class="line">    return config;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">const addEntryProxy &#x3D; () &#x3D;&gt; (configFunction) &#x3D;&gt; &#123;</span><br><span class="line">    multipleEntry.addEntryProxy(configFunction);</span><br><span class="line">    return configFunction;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    webpack: override(</span><br><span class="line">        addEntry(),</span><br><span class="line">    ),</span><br><span class="line">    devServer: overrideDevServer(</span><br><span class="line">        addEntryProxy(),</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
禁用 ManifestPlugin<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">const &#123; override, &#125; &#x3D; require(&#39;customize-cra&#39;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">const removeManifest &#x3D; () &#x3D;&gt; config &#x3D;&gt; &#123;</span><br><span class="line">    config.plugins &#x3D; config.plugins.filter(</span><br><span class="line">        p &#x3D;&gt; p.constructor.name !&#x3D;&#x3D; &quot;ManifestPlugin&quot;</span><br><span class="line">    );</span><br><span class="line">    return config;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    webpack: override(</span><br><span class="line">        removeManifest(),</span><br><span class="line">    ),</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
antd 按需加载 &amp;&amp; less-loader<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">const &#123; override, fixBabelImports, addLessLoader &#125; &#x3D; require(&#39;customize-cra&#39;);</span><br><span class="line"></span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    webpack: override(</span><br><span class="line">        fixBabelImports(&#39;import&#39;, &#123;</span><br><span class="line">            libraryName: &#39;antd&#39;,</span><br><span class="line">            libraryDirectory: &#39;es&#39;,</span><br><span class="line">            style: &#39;css&#39;,</span><br><span class="line">        &#125;),</span><br><span class="line">        addLessLoader(),</span><br><span class="line">    ),</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
antd-mobile PostCSS &amp;&amp; rem 配置</li>
</ol>
<p>移动端使用 rem 布局时，会借助 PostCSS 处理 px 到 rem 单位的转换。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">const &#123;</span><br><span class="line">    override,</span><br><span class="line">    addLessLoader,</span><br><span class="line">    addPostcssPlugins,</span><br><span class="line">    fixBabelImports,</span><br><span class="line">&#125; &#x3D; require(&quot;customize-cra&quot;);</span><br><span class="line"></span><br><span class="line">module.exports &#x3D; override(</span><br><span class="line">    addLessLoader(),</span><br><span class="line">    addPostcssPlugins([require(&quot;postcss-px2rem-exclude&quot;)(&#123;</span><br><span class="line">        remUnit: 16,</span><br><span class="line">        propList: [&#39;*&#39;],</span><br><span class="line">        exclude: &#39;&#39;</span><br><span class="line">    &#125;)]),</span><br><span class="line">    fixBabelImports(&#39;import&#39;, &#123;</span><br><span class="line">        libraryName: &#39;antd-mobile&#39;,</span><br><span class="line">        style: &#39;css&#39;,</span><br><span class="line">    &#125;),</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>需要注意的是和 addLessLoader 一起使用时，addPostcssPlugins 要放在后面，这和直接使用 webpack 配置是一样的顺序。</p>
<p>配置Proxy</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">const &#123; override, overrideDevServer &#125; &#x3D; require(&#39;customize-cra&#39;);</span><br><span class="line"></span><br><span class="line">const addProxy &#x3D; () &#x3D;&gt; (configFunction) &#x3D;&gt; &#123;</span><br><span class="line">    configFunction.proxy &#x3D; &#123;</span><br><span class="line">        &#39;&#x2F;v2ex&#x2F;&#39;: &#123;</span><br><span class="line">            target: &#39;https:&#x2F;&#x2F;www.v2ex.com&#39;,</span><br><span class="line">            changeOrigin: true,</span><br><span class="line">            pathRewrite: &#123; &#39;^&#x2F;v2ex&#39;: &#39;&#x2F;&#39; &#125;,</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    return configFunction;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module.exports &#x3D; &#123;</span><br><span class="line">    webpack: override(),</span><br><span class="line">    devServer: overrideDevServer(</span><br><span class="line">        addProxy()</span><br><span class="line">    )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2021-07-09T05:22:26.096Z" itemprop="dateUpdated">2021-07-09 13:22:26</time>
</span><br>


        
    </div>
    
    <footer>
        <a href="https://www.jscheng.top">
            <img src="/blog/img/avatar.png" alt="jscheng">
            jscheng
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/react/" rel="tag">react</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://www.jscheng.top/2020/06/02/react-init/&title=《creat-react-app 配置》 — jscheng&pic=https://www.jscheng.top/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://www.jscheng.top/2020/06/02/react-init/&title=《creat-react-app 配置》 — jscheng&source=jscheng 的博客" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <!--<li>-->
        <!--<a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://www.jscheng.top/2020/06/02/react-init/" data-title=" Facebook">-->
          <!--<i class="icon icon-facebook"></i>-->
        <!--</a>-->
      <!--</li>-->
      <!--<li>-->
        <!--<a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《creat-react-app 配置》 — jscheng&url=https://www.jscheng.top/2020/06/02/react-init/&via=https://www.jscheng.top" data-title=" Twitter">-->
          <!--<i class="icon icon-twitter"></i>-->
        <!--</a>-->
      <!--</li>-->
      <!--<li>-->
        <!--<a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://www.jscheng.top/2020/06/02/react-init/" data-title=" Google+">-->
          <!--<i class="icon icon-google-plus"></i>-->
        <!--</a>-->
      <!--</li>-->
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/2020/06/05/react-axios-token/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">对axios封装中，在登录token过期，返回401状态码，react-router怎么进行路由跳转</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/2020/05/28/vue-cli4-X/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">vue-cli4.X 建项目</h4>
      </a>
    </div>
  
</nav>



    











    <!-- Valine Comments -->
    <div class="comments vcomment" id="comments"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
    <script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
    <!-- Valine Comments script -->
    <script>
        var GUEST_INFO = ['nick','mail','link'];
        var guest_info = 'nick,mail'.split(',').filter(function(item){
          return GUEST_INFO.indexOf(item) > -1
        });
        new Valine({
            el: '#comments',
            notify: 'false' == 'true',
            verify: 'false' == 'true',
            appId: "hgOSdOMMl0QDNQ9A5Dd0BV74-gzGzoHsz",
            appKey: "6gJhVhXHxUAUmo2qsoqpUFpi",
            avatar: "mm",
            placeholder: "请输入",
            guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
            pageSize: "10"
        })
    </script>
    <!-- Valine Comments end -->










</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/blog/img/wechat.jpg" alt="打赏二维码">
        </div>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


       <!-- <p>
            
            <span>博客内容遵循</span>
        </p>-->
    </div>
    <div class="bottom">
        <p><span>jscheng &copy; 2020 - 2021</span>
            <span>
                
                <a href="http://www.miitbeian.gov.cn/" target="_blank">鲁ICP备18022674号-1</a><br>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://www.jscheng.top/2020/06/02/react-init/&title=《creat-react-app 配置》 — jscheng&pic=https://www.jscheng.top/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://www.jscheng.top/2020/06/02/react-init/&title=《creat-react-app 配置》 — jscheng&source=jscheng 的博客" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <!--<li>-->
        <!--<a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://www.jscheng.top/2020/06/02/react-init/" data-title=" Facebook">-->
          <!--<i class="icon icon-facebook"></i>-->
        <!--</a>-->
      <!--</li>-->
      <!--<li>-->
        <!--<a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《creat-react-app 配置》 — jscheng&url=https://www.jscheng.top/2020/06/02/react-init/&via=https://www.jscheng.top" data-title=" Twitter">-->
          <!--<i class="icon icon-twitter"></i>-->
        <!--</a>-->
      <!--</li>-->
      <!--<li>-->
        <!--<a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://www.jscheng.top/2020/06/02/react-init/" data-title=" Google+">-->
          <!--<i class="icon icon-google-plus"></i>-->
        <!--</a>-->
      <!--</li>-->
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://www.jscheng.top/2020/06/02/react-init/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '哪去了！';
            clearTimeout(titleTime);
        } else {
            document.title = 'jscheng';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
